﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RdH.XMLToolbox.Plugin;

namespace RdH.XMLToolbox
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class WindowXMLToolbox : Window
    {
        public WindowXMLToolbox()
        {
            InitializeComponent();
            this.MinHeight = 300;
            this.MinWidth = 400;
            txtBlockStatus.Text = "No Plugins found.";
            LoadPlugins();
        }

        private void LoadPlugins()
        {
            tabControlPlugins.Items.Clear();
            viewMenu.Items.Clear();
            pluginMenu.Items.Clear();

            foreach (IPlugin plugin in PluginLoader.LoadPlugins("Plugin"))
            {
                TabItem tab = new TabItem();
                tab.Header = plugin.Title;
                tab.ToolTip = plugin.Discription;
                tab.Content = new DockPanel();
                ((DockPanel)tab.Content).Children.Add(plugin.Control);
                ((DockPanel)tab.Content).LastChildFill = true;
                ((DockPanel)tab.Content).Margin = new Thickness(0, 0, 0, 0);
                tabControlPlugins.Items.Add(tab);

                MenuItem item = new MenuItem();
                item.IsCheckable = true;
                item.IsChecked = true;
                item.Header = plugin.Title;
                item.Tag = tab;
                item.Checked += new RoutedEventHandler(viewItem_Checked);
                item.Unchecked += new RoutedEventHandler(viewItem_Unchecked);
                viewMenu.Items.Add(item);

                pluginMenu.Items.Add(plugin.MenuItem);
            }

            if (PluginLoader.MinimumHeight + 100 > this.MinHeight) { this.MinHeight = PluginLoader.MinimumHeight + 100; }
            if (PluginLoader.MinimumWidth > this.MinWidth) { this.MinWidth = PluginLoader.MinimumWidth; }

            if (tabControlPlugins.Items.Count > 0) { tabControlPlugins.SelectedIndex = 0; }
        }

        private void viewItem_Checked(object sender, RoutedEventArgs e)
        {
            MenuItem item = (MenuItem)e.Source;
            TabItem tab = (TabItem)item.Tag;
            tab.Visibility = Visibility.Visible;
            ((DockPanel)tab.Content).Visibility = Visibility.Visible;
        }

        private void viewItem_Unchecked(object sender, RoutedEventArgs e)
        {
            MenuItem item = (MenuItem)e.Source;
            TabItem tab = (TabItem)item.Tag;
            tab.Visibility = Visibility.Hidden;
            ((DockPanel)tab.Content).Visibility = Visibility.Hidden;
        }

        private void exitChoice_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void reloadChoice_Click(object sender, RoutedEventArgs e)
        {
            LoadPlugins();
        }

        private void tabControlPlugins_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabItem tab = (TabItem)((TabControl)e.Source).SelectedItem;
            txtBlockStatus.Text = tab.ToolTip.ToString();
        }

    }
}
